package com.megvii.corelib.util;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by linyue on 15/12/27.
 */
public class FileUtil
{
    public static final int IO_BUFFER_SIZE = 65535;

    public static String getAbsoluteAlbumImagePath(Context context, Uri uri)
    {
        if (uri == null)
        {
            return null;
        }
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        if (uri != null && filePathColumn != null)
        {
            Cursor cursor = context.getContentResolver()
                    .query(uri, filePathColumn, null, null, null);
            if (cursor == null)
            {
                return null;
            }
            if (cursor.moveToFirst())
            {
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                // 获取照片地址
                String filePath = cursor.getString(columnIndex);
                return filePath;
            }
        }
        return null;
    }

    public static Bitmap readBitmapFromLocalFile(String filePath)
    {
        Bitmap bitmap = null;
        FileInputStream fis = null;
        try
        {
            fis = new FileInputStream(filePath);
            ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            copy(fis, dataStream);
            dataStream.flush();
            byte[] data = dataStream.toByteArray();
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            return bitmap;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            if (fis != null)
            {
                try
                {
                    fis.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void copy(InputStream in, OutputStream out) throws IOException
    {
        byte[] b = new byte[IO_BUFFER_SIZE];
        int read;
        while ((read = in.read(b)) != -1)
        {
            out.write(b, 0, read);
        }
    }

    /**
     * 判断文件是否存在
     * @param filePath 要进行判断的文件路径
     * @return  文件是否存在
     */
    public static boolean isExists(String filePath){
        if(filePath == null || filePath.equals("")){
            return false;
        }
        File file = new File(filePath);
        return file.exists();
    }

    /**
     * 根据传入的文件绝对路径获取文件名
     * @param fullName 文件绝对路径
     * @return 文件名
     */
    public static String getFileName(String fullName){
        String strResult = null;
        if(fullName != null){
            strResult = fullName.substring(fullName.lastIndexOf("/") + 1);
        }
        return strResult;
    }
}